package org.cyz.tools.common.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * @author Chenyz
 * @title: MacUtil
 * @projectName RedisGUI
 * @description: TODO
 * @date 2020年10月6日, 000610:02:44
 */
public class MacUtil {
    public static String getLocalMac()  {
        //得到IP，输出PC-201309011313/122.206.73.83
        InetAddress ia = null;
        StringBuffer sb=null;
        try {
            ia = InetAddress.getLocalHost();
            //获取网卡，获取地址
            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
            sb = new StringBuffer("");
            for(int i=0; i<mac.length; i++) {
                if(i!=0) {
                    sb.append("-");
                }
                //字节转换为整数
                int temp = mac[i]&0xff;
                String str = Integer.toHexString(temp);
                if(str.length()==1) {
                    sb.append("0"+str);
                }else {
                    sb.append(str);
                }
            }
        } catch (UnknownHostException | SocketException e) {
            e.printStackTrace();
        }
        return sb.toString().toUpperCase();
    }
}
